package com.axend.aerosense.login.utils;

import android.app.Activity;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.axend.aerosense.base.utils.LogUtils;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.util.WebUtil;
import com.axend.aerosense.login.R;

public class TextViewUtil {

    /**
     * 协议相关
     * @param textView
     */
    public static void initText(TextView textView) throws Exception{
        if(textView == null){
            return;
        }
        final SpannableStringBuilder style = new SpannableStringBuilder();
        String text = textView.getResources().getString(R.string.login_user_agreement);
        //设置文字
        style.append(text);

        //设置部分文字点击事件
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                String url = UrlApi.userProtocolUrl;
                String urlTitle = textView.getContext().getString(R.string.user_title_user_agreement);
                WebUtil.openWebView(textView.getContext().getApplicationContext(), url, urlTitle);

            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);

            }
        };
        int start = text.indexOf("《");
        int end = text.indexOf("》");
        int lastStart = text.lastIndexOf("《");
        int lastEnd = text.lastIndexOf("》");
        if(start < 0){
            // 不是中文
            start = 29;
        }

        if(end < 0){
            // 不是中文
            end = 43;
        }else{
            end ++ ;
        }

        if(lastStart < 0){
            // 不是中文
            lastStart = 48;
        }

        if(lastEnd < 0){
            // 不是中文
            lastEnd = 62;
        }else{
            lastEnd++;
        }

        style.setSpan(clickableSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(style);
        ClickableSpan clickableSpan2 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                String url = UrlApi.privacyProtocolUrl;
                String urlTitle = textView.getContext().getString(R.string.user_agreement_2);;
                WebUtil.openWebView(textView.getContext().getApplicationContext(), url, urlTitle);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);

            }
        };

        style.setSpan(clickableSpan2, lastStart, lastEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        //设置部分文字颜色
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#3578FC"));
        style.setSpan(foregroundColorSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ForegroundColorSpan foregroundColorSpan1 = new ForegroundColorSpan(Color.parseColor("#3578FC"));
        style.setSpan(foregroundColorSpan1, lastStart, lastEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //配置给TextView
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setText(style);
    }
}
